import { Module } from '@nestjs/common'
import { TypeOrmModelsHost } from '@boticord/startup'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { TypeOrmModule } from '@nestjs/typeorm'
import { MigratorService } from './migrator.service'
import { DiscordooModule } from '@boticord/discordoo'
import { ClientBuilder, IntentsUtil } from 'discordoo'

@Module({
  imports: [
    ConfigModule.forRoot({
      ignoreEnvFile: true,
    }),
    TypeOrmModule.forRootAsync({
      inject: [ ConfigService ],
      imports: [ ConfigModule ],
      useFactory: (cfg: ConfigService) => ({
        type: 'postgres',
        database: cfg.getOrThrow<string>('POSTGRES_DATABASE'),
        port: +cfg.getOrThrow<string>('POSTGRES_PORT'),
        username: cfg.getOrThrow('POSTGRES_USERNAME'),
        password: cfg.getOrThrow('POSTGRES_PASSWORD'),
        host: cfg.getOrThrow('POSTGRES_HOST'),
        autoLoadEntities: true,
        synchronize: true,
      }),
    }),
    DiscordooModule.forRootAsync({
      imports: [ ConfigModule ],
      inject: [ ConfigService ],
      useFactory: (config: ConfigService) => ({
        // BOT CONFIG IS HERE
        build: (b: ClientBuilder) => b
          .gateway({
            // specify all intents
            intents: IntentsUtil.NON_PRIVILEGED_NUMERIC,
          }),
        discordToken: config.getOrThrow<string>('DISCORD_TOKEN'),
      }),
    }),
    TypeOrmModelsHost,
  ],
  providers: [ MigratorService ],
})
export class MigratorModule {}
